package com.easy.work.function.main;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.andview.refreshview.utils.Utils;
import com.base.syslib.Logs;
import com.base.syslib.util.DensityUtil;
import com.base.syslib.util.MessageTools;
import com.base.syslib.view.MyListView;
import com.base.syslib.view.ObservableScrollView;
import com.easy.work.R;
import com.easy.work.app.BaseActivity;
import com.easy.work.bean.CompanyBriefBean;
import com.easy.work.function.main.adapter.CompanyBriefAdapter;
import com.easy.work.function.main.adapter.CompanyPicturePageAdapter;
import com.easy.work.http.BaseResponse;
import com.easy.work.http.HttpClient;
import com.easy.work.http.request.CompanyBriefRequest;
import com.easy.work.utils.OpenMap;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * @author zhangfeng
 * @date 2018/01/11
 * @desc
 */

public class CompanyBriefActivity extends BaseActivity implements ObservableScrollView.ScrollViewListener {

    private static final String TAG = CompanyBriefActivity.class.getSimpleName();
    private static final String COMPANY_ID = "companyId";
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.tv_picture_number)
    TextView tvPictureNumber;
    @BindView(R.id.tv_work_need)
    TextView tvWorkNeed;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_distance)
    TextView tvDistance;
    @BindView(R.id.listView)
    MyListView listView;
    @BindView(R.id.tv_look_more)
    TextView tvLookMore;
    @BindView(R.id.tv_introduce)
    TextView tvIntroduce;
    @BindView(R.id.scrollView)
    ObservableScrollView scrollView;
    @BindView(R.id.rl_back)
    RelativeLayout rlBack;
    @BindView(R.id.title_txt)
    TextView titleTxt;
    @BindView(R.id.titleLayout)
    RelativeLayout titleLayout;

    public static void start(Context ctx, String companyId) {
        ctx.startActivity(new Intent(ctx, CompanyBriefActivity.class)
                .putExtra(COMPANY_ID, companyId));
    }


    private String companyId;
    private int height;

    private CompanyBriefAdapter mAdapter;
    private List<CompanyBriefBean.WorkBaseListEntity> overList = new ArrayList<>();
    /**
     * title变色高度
     */
    private int length;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_company_breif);
        ButterKnife.bind(this);

        titleTxt.setAlpha(0);
//        height = DensityUtil.dip2px(this, 210);
        scrollView.setScrollViewListener(this);
        rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

//        int statusHeight = Utils.getStatusHeight(this);
//        if (statusHeight == -1) {
//            statusHeight = DensityUtil.dip2px(this, 25);
//        }
//        lyTop.setPadding(0, statusHeight, 0, 0);

        viewPager.setFocusable(true);
        viewPager.setFocusableInTouchMode(true);
        viewPager.requestFocus();
        ViewTreeObserver vto = viewPager.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                viewPager.getViewTreeObserver().removeGlobalOnLayoutListener(
                        this);
                length = viewPager.getHeight();

                scrollView.setScrollViewListener(CompanyBriefActivity.this);
            }
        });
        List<CompanyBriefBean.WorkBaseListEntity> list = new ArrayList<>();
        mAdapter = new CompanyBriefAdapter(this, 1, list);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CompanyBriefBean.WorkBaseListEntity workBaseListEntity = mAdapter.getItem(position);
                WorkBaseDetailAct.start(oThis, workBaseListEntity.workId);
            }
        });

        companyId = getIntent().getStringExtra(COMPANY_ID);


        getCompanyBrief();
    }

    private void getCompanyBrief() {
        showDialog();
        final CompanyBriefRequest request = new CompanyBriefRequest();
        request.companyId = companyId;
        Logs.e(TAG, "companyId:" + companyId);
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                Logs.e(TAG, "response:" + response.toString());
                closeDialog();
                handleData(response);
            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                closeDialog();
                MessageTools.showToast(getApplicationContext(), msg);
            }
        });
    }

    private void handleData(BaseResponse response) {
        final CompanyBriefBean bean = (CompanyBriefBean) response;
        Logs.e(TAG, "111");
        if (bean == null) {
            Logs.e(TAG, "222");
            return;
        }
        Logs.e(TAG, "333");
        Logs.e(TAG, "bean.companyName:" + bean.companyName);
        // 文本显示
        tvWorkNeed.setText(getStr(bean.companyName));
        tvAddress.setText(getStr(bean.provinceText) + getStr(bean.cityText) +
                getStr(bean.areaText) + getStr(bean.companyAddress));
        tvAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //导航
                OpenMap openMap = new OpenMap();
                openMap.openBaiDuMap(oThis, bean.companyName, bean.latitude, bean.longitude);
            }
        });
//        tvDistance.setText("<820m");
        tvDistance.setText(bean.distance);
        tvIntroduce.setText(getStr(bean.companyIntroduction));
        titleTxt.setText(bean.companyName);
        // 公司图片显示
        List<CompanyBriefBean.PictureListEntity> picList = bean.pictureList;
        if (picList != null && !picList.isEmpty()) {
            Logs.e(TAG, "pic_size:" + picList.size());
            tvPictureNumber.setText(String.valueOf(picList.size()));
            List<View> list = new ArrayList<>();
            for (CompanyBriefBean.PictureListEntity entity : picList) {
                View v = getLayoutInflater().inflate(R.layout.item_company_pic, null);
                ImageView iv = (ImageView) v.findViewById(R.id.iv_pic);
                Picasso.with(this).load(entity.companyImage).into(iv);
                list.add(v);
            }
            viewPager.setAdapter(new CompanyPicturePageAdapter(list));
        }

        // 职位列表显示
        List<CompanyBriefBean.WorkBaseListEntity> jobList = bean.workBaseList;
        if (jobList != null && !jobList.isEmpty()) {
            int size = jobList.size();
            Logs.e(TAG, "job_size:" + size);
            if (size > 3) {
                tvLookMore.setText("更多" + (size - 3) + "个职位");
                overList.addAll(jobList);
                mAdapter.refreshData(jobList.subList(0, 3));
            } else {
                tvLookMore.setText("更多职位");
                mAdapter.refreshData(jobList);
            }
        } else {
            tvLookMore.setText("更多职位");
        }
    }

    @OnClick(R.id.tv_look_more)
    public void lookMore() {
        String text = tvLookMore.getText().toString();
        if ("更多职位".equals(text)) {
            MessageTools.showToast(getApplicationContext(), "没有更多职位了");
            return;
        }
        int size = overList.size();
        if ("收起".equals(text)) {
            tvLookMore.setText("更多" + (size - 3) + "个职位");
            tvLookMore.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_xia, 0, 0);
            mAdapter.refreshData(overList.subList(0, 3));
        } else {
            tvLookMore.setText("收起");
            tvLookMore.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_shang, 0, 0);
            mAdapter.addMoreData(overList.subList(3, size));
        }
    }

    private String getStr(String text) {
        return TextUtils.isEmpty(text) ? "" : text;
    }

    @Override
    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
        if (y <= 0) {
            titleLayout.setBackgroundColor(Color.argb((int) 0, 255, 144, 00));//AGB由相关工具获得，或者美工提供
            titleTxt.setAlpha(0);
//        } else if (y > 0 && y <= length) {
//            float scale = (float) y / length;
//            float alpha = (255 * scale);
//            // 只是layout背景透明(仿知乎滑动效果)
//            titleLayout.setBackgroundColor(Color.argb((int) alpha, 255, 144, 00));
//            titleTxt.setAlpha(alpha / 255);
        } else {
            titleLayout.setBackgroundColor(Color.argb((int) 255, 255, 144, 00));
            titleTxt.setAlpha(1);
        }
    }
}
